﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.BLL;
using Wp.DAL;

public partial class Admin_Modules_Categories_CategoryEdit : System.Web.UI.Page
{
    public static string Taxonomy = "category";
    public int Id { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        List<TblTermTaxonomyEntity> ls = new List<TblTermTaxonomyEntity>();
        Id = CommonHelper.QueryStringInt("Id", -1);
        if (Id == -1)
        {
            Response.Redirect("~/Admin/Modules/Categories/Default.aspx");
        }
        if (!IsPostBack)
        {
            LoadDDL();
            LoadData();
        }
    }
    void LoadData()
    {
        TblTermTaxonomyEntity item = TblTermTaxonomyManager.CreateInstant().SelectOne(Id);
        if (item != null)
        {
            txtName.Text = item.Name;
            txtSlug.Text = item.Slug;
            ddlparent.SelectedValue = item.ParentId.ToString();
            txtDescription.Text = item.Description;
        }
    }
    void LoadDDL()
    {
        ddlparent.DataSource = TblTermTaxonomyManager.CreateInstant().SelectTblTermsTaxonomyGetChildFormat(null, Taxonomy); ;
        ddlparent.DataTextField = "Name";
        ddlparent.DataValueField = "Id";
        ddlparent.DataBind();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        TblTermTaxonomyEntity item = TblTermTaxonomyManager.CreateInstant().SelectOne(Id);
        int result = TblTermTaxonomyManager.CreateInstant().UpdateTag(item.Id, txtName.Text, txtSlug.Text, ddlparent.SelectedValue.ToInt(), txtDescription.Text, Taxonomy);
        if (result == -1)
        {
            lblError.Text = "An error occurred while updating the entries";
        }
        else if (result == -2)
        {
            lblError.Text = "A term with the name provided already exists";
        }
        else
            lblError.Text = "Data was updated successfully";

    }
}